<template>
<el-empty></el-empty>
</template>

<script>
  import { Splitpanes, Pane } from 'splitpanes'
  import 'splitpanes/dist/splitpanes.css'
  import ProjectDocumentForm from './ProjectDocumentForm'
  import ProjectDocumentFolderForm from './ProjectDocumentFolderForm'
  import projectDocumentService from '@/api/document/projectDocumentService'
  import projectDocumentFolderService from '@/api/document/projectDocumentFolderService'
  export default {
    data () {
      return {
        searchVisible: true,
        searchForm: {
          folder: {
            id: ''
          },
          documentName: ''
        },
        filterText: '',
        projectDocumentFolderTreeData: [],
        selectProjectDocumentFolderName: '',
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: [{ column: "a.create_time", asc: false }],
        },
        loading: false
      }
    },
    components: {
      Splitpanes,
      Pane,
      ProjectDocumentForm,
      ProjectDocumentFolderForm
    },
    mounted () {
      this.$nextTick(() => {
        // 将表格和工具栏进行关联
        const $table = this.$refs.projectDocumentTable
        const $toolbar = this.$refs.projectDocumentToolbar
        $table.connect($toolbar)
      })
    },
    activated () {
      this.refreshTree()
      this.refreshList()
    },
    computed: {
    },
    watch: {
      filterText (val) {
        this.$refs.projectDocumentFolderTree.filter(val)
      }
    },
    methods: {
      filterNode (value, data) {
        if (!value) return true
        return data.name.indexOf(value) !== -1
      },
      refreshTree () {
        projectDocumentFolderService.treeData().then((data) => {
          this.projectDocumentFolderTreeData = data
        })
      },
      handleNodeClick (data) {
        this.searchForm.folder.id = data.id
        this.refreshList()
      },
      // 新增根节点
      addTreeNode (data) {
        var sort = 30
        if (data) {
           if (data.children && data.children.length > 0) {
            sort = data.children[data.children.length - 1].sort + 30
           }
        } else if (this.projectDocumentFolderTreeData && this.projectDocumentFolderTreeData.length > 0) {
           sort = this.projectDocumentFolderTreeData[this.projectDocumentFolderTreeData.length - 1].sort + 30
        }
        var newProjectDocumentFolderTreeData = {
           id: '',
           parent: {
             id: data ? data.id : "0",
           },
           sort: sort
          }
        if (data) {
            this.$refs.projectDocumentFolderForm.init('addChild', newProjectDocumentFolderTreeData)
        } else {
            this.$refs.projectDocumentFolderForm.init('add', newProjectDocumentFolderTreeData)
        }
      },
      // 修改
      editTreeNode (id) {
        this.$refs.projectDocumentFolderForm.init('edit', {id: id, parent: {id: '', name: ''}})
      },
      delTreeNode (ids) {
        if(!ids){
          var CheckedNodes = this.$refs.projectDocumentFolderTree.getCheckedNodes()
          if(CheckedNodes.length == 0){
            this.$message.warning("请选择需要删除的项")
            return false;
          }
          ids = CheckedNodes.map(item => item.id).join(',')
        }
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          projectDocumentFolderService.delete(ids).then((data) => {
            this.$message.success(data)
            this.loading = false
            this.refreshTree()
            this.refreshList()
          })
        })
      },
      //树拖拽
      nodeDrop(draggingNode, dropNode, dropType){
        this.loading = true
        projectDocumentFolderService.drag({draggingNode: draggingNode.data, dropNode: dropNode.data, dropType: dropType}).then((data)=>{
            this.$message(data)
            this.loading = false
        })
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        projectDocumentService.list({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm
        }).then((data) => {
          this.dataList = data.records
          this.tablePage.total = data.total
          this.loading = false
        })
      },
      // 当前页
      currentChangeHandle ({ currentPage, pageSize }) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.refreshList()
      },
      // 排序
	  sortChangeHandle(obj) {
        this.tablePage.orders = [];
        if (obj.order != null) {
          this.tablePage.orders = [{ column: obj.column.sortBy || this.$utils.toLine(obj.property),	asc: obj.order === "asc" }];
        } else {
          this.tablePage.orders = [{ column: "a.create_time", asc: false }];
        }
        this.refreshList();
      },
      // 新增
      add () {
        this.$refs.projectDocumentForm.init('add', '')
      },
      // 修改
      edit (id) {
        id = id || this.$refs.projectDocumentTable.getCheckboxRecords().map(item => {
          return item.id
        })[0]
        this.$refs.projectDocumentForm.init('edit', id)
      },
      // 查看
      view (id) {
        this.$refs.projectDocumentForm.init('view', id)
      },
      // 删除
      del (id) {
        let ids = id || this.$refs.projectDocumentTable.getCheckboxRecords().map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          projectDocumentService.delete(ids).then((data) => {
            this.$message.success(data)
            this.refreshList()
            this.loading = false
          })
        })
      },
      // 下载模板
      downloadTpl() {
			this.loading = true
			projectDocumentService
				.exportTemplate()
				.then((data) => {
					// 将二进制流文件写入excel表，以下为重要步骤
					this.$utils.downloadExcel(data, "项目文件导入模板");
					this.loading = false
				})
				.catch(function (err) {
				    this.loading = false
					if (err.response) {
						console.log(err.response);
					}
				});
		},
		// 自定义服务端导入
		importMethod({ file }) {
			// 处理表单
			const formBody = new FormData();
			formBody.append("file", file);
			this.loading = true
			projectDocumentService.importExcel(formBody).then((result) => {
				this.$message.success({
					dangerouslyUseHTMLString: true,
					message: result,
				});
				this.refreshList();
			});
		},
		// 自定义服务端导出
		exportMethod({ options }) {
			// 传给服务端的参数
			const params = {
				current: this.tablePage.currentPage,
				size: this.tablePage.pageSize,
				orders: this.tablePage.orders,
				...this.searchForm,
				filename: options.filename,
				sheetName: options.sheetName,
				isHeader: options.isHeader,
				original: options.original,
				mode: options.mode,
				selectIds:
					options.mode === "selected"
						? options.data.map((item) => item.id)
						: [],
				exportFields: options.columns.map(
					(column) => column.property && column.property.split(".")[0]
				),
			};
			this.loading = true;
			return projectDocumentService
				.exportExcel(params)
				.then((data) => {
					// 将二进制流文件写入excel表，以下为重要步骤
					this.$utils.downloadExcel(data, options.filename);
					this.loading = false;
				})
				.catch(function (err) {
					if (err.response) {
						console.log(err.response);
					}
				});
		},
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.filterText = ''
        this.$refs.projectDocumentFolderTree.setCurrentKey(null)
        this.refreshList()
      }
    }
  }
</script>

